#coding=utf-8

from uliweb.orm import *
from uliweb.utils.common import get_var
import datetime

def get_modified_user():
    from uliweb import request
    
    return request.user.id

class SysFunctionCategory(Model):
    name = Field(str, max_length=40, verbose_name='系统功能点分类', required=True)
    system = Reference('system', verbose_name='所属系统', required=True, collection_name='sys_categories')
    
    class AddForm:
        fields = ['name']
        
    def __unicode__(self):
        return self.name

class SysFunction(Model):
    __verbose_name__ = u'系统功能点'
    
    category = Reference(SysFunctionCategory, verbose_name='功能点分类')
    sequence_number = Field(str, verbose_name='功能点编号', max_length=255, required=True)
    system = Reference('system', verbose_name='所属系统', required=True, collection_name='sys_functions')
    function_name = Field(str, verbose_name='功能名称', max_length=255, required=True)
    function_code = Field(str, verbose_name='功能代码', max_length=255, required=True)
    function_type = Field(CHAR, verbose_name='功能类型', max_length=1, choices=get_var('PARA/FUNCTION_TYPE'), required=True)
    business_rules = Field(TEXT, verbose_name='业务规则')
#    change_status = Field(CHAR, verbose_name='变动状态', max_length=1, choices=get_var('PARA/CHANGE_STATUS'), required=True)
    business_degree = Field(CHAR, verbose_name='业务重要程度', max_length=1, choices=get_var('PARA/BUSINESS_DEGREE'), required=True)
    
    trading_route_origin = Field(str, verbose_name='交易路径上游', max_length=255)
    route_origin_code = Field(str, verbose_name='上游接口代码', max_length=255)
    trading_route_downstream = Field(str, verbose_name='交易路径下游', max_length=255)
    route_downstream_code = Field(str, verbose_name='下游接口代码', max_length=255)
    
    trading_frequency = Field(bool, verbose_name='是否属于访问频度高的交易', choices=get_var('PARA/YESNO')) 
    daily_visit = Field(int, verbose_name='日均访问量')
    creator_user = Field(str, verbose_name='创建人', max_length=40)
    
    subsystem_abbr = Field(str, verbose_name='子系统英文简写', max_length=20)
    subsystem_name = Field(str, verbose_name='子系统中文名称', max_length=255)
    created_date = Field(datetime.datetime, verbose_name='创建时间', auto_now_add=True)
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True, collection_name='modified_user_functions')
    status = Field(CHAR, verbose_name='启用状态', default='1', max_length=1, choices=get_var('PARA/FUNCTION_USING_STATUS'), required=True)
    
    last_version_date = Field(datetime.date, verbose_name='最近上线时间')
    last_sittest_date = Field(datetime.date, verbose_name='最近SIT测试时间')
    last_uattest_date = Field(datetime.date, verbose_name='最近UAT测试时间')
    last_vttest_date = Field(datetime.date, verbose_name='最近版本检测时间')
    
    def __unicode__(self):
        return "[%s]%s" % (self.function_code, self.function_name)
    
    def get_url(self):
        return '<a href="/systems/functions/view/%d">%s</a>' % (self.id, self.function_name)
    
    class Table:
        fields = [
                {'name':'category'},#, 'width':120},
                {'name':'sequence_number'},
                {'name':'system'},
                {'name':'function_name'},
                {'name':'function_code'},
                {'name':'function_type'},
                {'name':'business_rules'},
#                {'name':'change_status'},
                {'name':'business_degree'},
                {'name':'trading_route_origin'},
                {'name':'route_origin_code'},
                {'name':'trading_route_downstream'},
                {'name':'route_downstream_code'},
                {'name':'trading_frequency'},
                {'name':'daily_visit'},
                {'name':'subsystem_abbr'},
                {'name':'subsystem_name'},
                {'name':'last_version_date'},
                {'name':'last_sittest_date'},
                {'name':'last_uattest_date'},
                {'name':'last_vttest_date'},
                {'name':'status'},
                {'name':'created_date'},
                {'name':'modified_date'},
                {'name':'modified_user'},
                {'name':'creator_user'}
                ]